package app;

import java.util.Map;

import org.apache.struts2.interceptor.RequestAware;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class EmployeeAction implements RequestAware, ModelDriven<Employee>, Preparable{


	private Dao dao=new Dao();
	
	private Employee employee;
	
	private Integer employeeId;
	
	public void setEmployeeId(Integer employeeId) {
		this.employeeId = employeeId;
	}
	
	public void setEmployee(Employee employee) {
		this.employee = employee;
	}
	
	@Override
	public void prepare() throws Exception {
		System.out.println("prepare..");
	}

	@Override
	public Employee getModel() {

		return employee;
	}

	private Map<String,Object> request;
	
	@Override
	public void setRequest(Map<String, Object> arg0) {
		this.request=arg0;
	}
	
	public String list() {
		
		request.put("emps", dao.getEmployees());
		return "list";
		
	}

	public void prepareSave() {
		employee=new Employee();
	}
	
	public String save() {
		dao.save(employee);
		return "success";
		
	}
	
	public String delete() {
		dao.delete(employeeId);
		return "success";
		
	}
	
	public void prepareEdit() {
		employee=dao.get(employeeId);
	}
	
	public String edit() {
		
		return "edit";
		
	}
	
	public void prepareUpdate() {
		employee=new Employee();
	}
	
	public String update() {
		
		dao.update(employee);
		return "success";
		
	}
	
}
